﻿Public Class FrmNhaCungCap

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Them()
    End Sub

    Sub LoadLenLuoi()
        Dim ll As DataTable
        ll = NhaCungCapDAO.SelectAll()
        dgvDSNhaCungCap.DataSource = ll

    End Sub


    Private Sub dgvDSNhaCungCap_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDSNhaCungCap.CellContentClick
        LoadLenLuoi()
    End Sub

    Private Sub FrmNhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadLenLuoi()
    End Sub
    Sub Them()
        Dim ncc As NhaCungCapDTO = New NhaCungCapDTO()
        ncc.TenNhaCungCap = txtTenNhaCungCap.Text
        ncc.DiaChi = txtDiaChi.Text
        ncc.DienThoai = txtDienThoai.Text
        ncc.Email = txtEmail.Text
        ncc.Fax = txtFax.Text

        NhaCungCapDAO.Insert(ncc)
        LoadLenLuoi()
    End Sub

    Sub Xoa()
        Dim ncc As NhaCungCapDTO = New NhaCungCapDTO()
        ncc.MaNhaCungCap = Convert.ToInt32(txtMaNhaCungCap.Text)
        NhaCungCapDAO.Delete(ncc)
        LoadLenLuoi()

    End Sub
    Sub Sua()
        Dim ncc As NhaCungCapDTO = New NhaCungCapDTO()
        ncc.MaNhaCungCap = txtMaNhaCungCap.Text
        ncc.TenNhaCungCap = txtTenNhaCungCap.Text
        ncc.DiaChi = txtDiaChi.Text
        ncc.DienThoai = txtDienThoai.Text
        ncc.Email = txtEmail.Text
        ncc.Fax = txtFax.Text

        NhaCungCapDAO.Update(ncc)
        LoadLenLuoi()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Xoa()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Sua()
    End Sub
    Sub HienThiChinhSua()
        If (dgvDSNhaCungCap.SelectedRows.Count > 0) Then
            txtMaNhaCungCap.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(0).Value.ToString()
            txtTenNhaCungCap.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(1).Value.ToString()
            txtDiaChi.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(2).Value.ToString()
            txtDienThoai.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(3).Value.ToString()
            txtEmail.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(4).Value.ToString()
            txtFax.Text = dgvDSNhaCungCap.SelectedRows(0).Cells(5).Value.ToString()
        End If
    End Sub

    Private Sub dgvDSNhaCungCap_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSNhaCungCap.SelectionChanged
        HienThiChinhSua()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtMaNhaCungCap.Text = ""
        txtTenNhaCungCap.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        txtEmail.Text = ""
        txtFax.Text = ""
    End Sub
End Class